package org.lyl.taobao.controller;

import org.lyl.taobao.api.IUserService;
import org.lyl.taobao.api.entity.User;
import org.lyl.taobao.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author: Re
 * @Date: 2021/4/1 21:21
 */
@WebServlet("/updateUser")
public class UpdateUserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;utf-8");
        String updateUser = req.getParameter("updateUser");
        if (updateUser.equals("updateUser")) {
            req.getRequestDispatcher("updateUser.jsp").forward(req,resp);
        }else if (updateUser.equals("rightUpdate")){
            HttpSession session = req.getSession();
            User user = (User) session.getAttribute("user");
            String password1 = req.getParameter("password1");
            String password2 = req.getParameter("password2");
            if (!password1.equals(password2)){
                req.setAttribute("msg","两次输入密码不同，请重新输入");
                req.getRequestDispatcher("updateUser.jsp").forward(req,resp);
                return;
            } else {
                user.setUserPassword(password1);
                IUserService iUserService = new UserServiceImpl();
                if (iUserService.updateUser(user)){
                    req.setAttribute("msg","密码已修改");
                    req.getRequestDispatcher("registerUser.jsp").forward(req,resp);
                } else {
                    req.setAttribute("msg","密码更改失败");
                    req.getRequestDispatcher("registerUser.jsp").forward(req,resp);
                }
            }
        }
    }
}
